﻿/* 2011/11/25 */
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cosmos.Windows.Forms.FileList;
using Cosmos.Windows.Forms.FileList.DynamicColumn;
using PictureManager.Option;

namespace PictureManager
{
    public partial class FileNameAttributeControl : UserControl
    {
        private ExtractionRuleListViewItem _lastSelectedItem;

        public FileNameAttributeControl()
        {
            InitializeComponent();

            fileNameAttributeToolTip.AutomaticDelay = 5000;
            fileNameAttributeToolTip.InitialDelay = 1000;
            fileNameAttributeToolTip.ReshowDelay = 500;
            fileNameAttributeToolTip.ShowAlways = true;
            fileNameAttributeToolTip.UseFading = true;
            fileNameAttributeToolTip.SetToolTip(fileNamePatternTextBox, "文件属性描述格式为 <属性名称:显示的列名:列宽>");

            _lastSelectedItem = null;
        }

        public void LoadSettings(StringCollection extractionRules, int fileNameAttributeIndex)
        {
            extractionRuleListView.Items.Clear();
            foreach (string aRule in extractionRules)
            {
                DynamicColumnExtractionRule rule = DynamicColumnExtractionRuleSerializer.Deserialize(aRule);                

                if (rule == null)
                {
                    rule = new DynamicColumnExtractionRule()
                    {
                        Extractor = DynamicColumnExtractionRule.RegexExtractorName,
                        Pattern = aRule,
                        ColumnDefinitions = new DynamicColumnDefinitionInfo[0],
                    };
                }

                ExtractionRuleListViewItem theItem = new ExtractionRuleListViewItem(rule);
                extractionRuleListView.Items.Add(theItem);
            }

            int theIndex = fileNameAttributeIndex;
            if (theIndex >= 0 && theIndex < extractionRuleListView.Items.Count)
                extractionRuleListView.Items[theIndex].Checked = true;
        }

        public void SaveSettings(StringCollection extractionRules, out int extractionRuleIndex)
        {
            if (_lastSelectedItem != null)
            {
                SaveColumnDefinitions(_lastSelectedItem);
            }

            extractionRules.Clear();
            ListView.ListViewItemCollection items = extractionRuleListView.Items;
            extractionRuleIndex = -1;
            foreach (ListViewItem anItem in items)
            {
                ExtractionRuleListViewItem item = (ExtractionRuleListViewItem)anItem;
                if (item.Checked)
                {
                    extractionRuleIndex = items.IndexOf(item);
                }

                string ruleText = DynamicColumnExtractionRuleSerializer.Serialize(item.Rule);                

                if (ruleText != null)
                {
                    extractionRules.Add(ruleText);
                }
            }
        }

        private void LoadColumnDefinitions(ExtractionRuleListViewItem ruleItem)
        {
            columnDefinitionListView.Items.Clear();
            foreach (DynamicColumnDefinitionInfo aDefinition in ruleItem.ColumnDefinitions)
            {
                ColumnDefinitionListViewItem item = new ColumnDefinitionListViewItem(aDefinition);
                columnDefinitionListView.Items.Add(item);
            }
        }

        private void SaveColumnDefinitions(ExtractionRuleListViewItem ruleItem)
        {
            ListView.ListViewItemCollection items = columnDefinitionListView.Items;
            List<DynamicColumnDefinitionInfo> definitionList = new List<DynamicColumnDefinitionInfo>(items.Count);
            foreach (ListViewItem anItem in items)
            {
                ColumnDefinitionListViewItem item = (ColumnDefinitionListViewItem)anItem;
                definitionList.Add(item.ColumnDefinition);
            }
            ruleItem.ColumnDefinitions = definitionList.ToArray();
        }

        private void splitContainer_SizeChanged(object sender, EventArgs e)
        {
            int distance = splitContainer.Height / 2;
            if (distance >= splitContainer.Panel1MinSize &&
                distance < (splitContainer.Width - splitContainer.Panel2MinSize))
            {
                splitContainer.SplitterDistance = distance;
            }
        }

        private void extractionRuleAddButton_Click(object sender, EventArgs e)
        {
            string fileNameAttributePattern = fileNamePatternTextBox.Text;
            if (String.IsNullOrEmpty(fileNameAttributePattern))
            {
                MessageBox.Show("请输入有效的内容", "添加失败");
                return;
            }

            string extractorName = null;
            IDynamicAttributeExtractor extractor = null;
            try
            {
                if (splitRadioButton.Checked)
                {
                    extractorName = DynamicColumnExtractionRule.SpliterExtractorName;
                    extractor = new SpliterFileNameAttributeExtractor(fileNameAttributePattern);
                }
                else
                {
                    extractorName = DynamicColumnExtractionRule.RegexExtractorName;
                    extractor = new RegexFileNameAttributeExtractor(fileNameAttributePattern, null);
                }

                /*ListView.ListViewItemCollection items = fileNameAttributeListView.Items;
                foreach (ListViewItem aItem in items)
                {
                    if (aItem.SubItems[0].Text.Equals(fileNameAttributePattern) &&
                        aItem.SubItems[1].Text.Equals(extractorName))
                    {
                        MessageBox.Show("已存在此模式", "添加失败");
                        return;
                    }
                }*/
            }
            catch (Exception)
            {
                MessageBox.Show("模式内容无效", "添加失败");
                return;
            }

            ExtractionRuleListViewItem item = new ExtractionRuleListViewItem(fileNameAttributePattern, extractorName);
            extractionRuleListView.Items.Add(item);
        }

        private void extractionRuleRemoveButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = extractionRuleListView.SelectedItems;
            if (items.Count != 0)
                foreach (ListViewItem aItem in items)
                    extractionRuleListView.Items.Remove(aItem);
            else
                MessageBox.Show("请选择需要删除的项目", "删除失败");
        }

        private void extractionRuleModifyButton_Click(object sender, EventArgs e)
        {
            string fileNameAttributePattern = fileNamePatternTextBox.Text;
            if (String.IsNullOrEmpty(fileNameAttributePattern))
            {
                MessageBox.Show("请输入有效的内容", "修改失败");
                return;
            }

            ListView.SelectedListViewItemCollection items = extractionRuleListView.SelectedItems;
            if (items.Count == 1)
            {
                /*foreach (ListViewItem aItem in fileNameAttributeListView.Items)
                {
                    if (aItem.Text.Equals(fileNameAttributeTextBox.Text))
                    {
                        MessageBox.Show("已存在此模式", "修改失败");
                        return;
                    }
                }*/

                string extractorName = null;
                IDynamicAttributeExtractor extractor = null;
                try
                {
                    if (splitRadioButton.Checked)
                    {
                        extractorName = DynamicColumnExtractionRule.SpliterExtractorName;
                        extractor = new SpliterFileNameAttributeExtractor(fileNameAttributePattern);
                    }
                    else
                    {
                        extractorName = DynamicColumnExtractionRule.RegexExtractorName;
                        extractor = new RegexFileNameAttributeExtractor(fileNameAttributePattern, null);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("模式内容无效", "修改失败");
                    return;
                }

                ExtractionRuleListViewItem item = (ExtractionRuleListViewItem)items[0];
                item.Pattern = fileNameAttributePattern;
                item.ExtractorName = extractorName;
            }
            else
            {
                MessageBox.Show("请选择一项修改", "修改失败");
                return;
            }
        }

        private void extractionRuleUpButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedItems = extractionRuleListView.SelectedItems;
            if (selectedItems.Count == 0)
            {
                return;
            }

            int insertIndex = selectedItems[0].Index;
            if (insertIndex > 0)
            {
                insertIndex--;
            }

            List<ListViewItem> moveItems = new List<ListViewItem>(selectedItems.Count);
            foreach (ListViewItem anItem in selectedItems)
            {
                moveItems.Add(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                extractionRuleListView.Items.Remove(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                extractionRuleListView.Items.Insert(insertIndex, anItem);
                insertIndex++;
            }
        }

        private void extractionRuleDownButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedItems = extractionRuleListView.SelectedItems;
            if (selectedItems.Count == 0)
            {
                return;
            }

            int insertIndex = selectedItems[selectedItems.Count - 1].Index;
            if (insertIndex < extractionRuleListView.Items.Count - 1)
            {
                insertIndex++;
            }
            insertIndex++;
            insertIndex = insertIndex - selectedItems.Count;

            List<ListViewItem> moveItems = new List<ListViewItem>(selectedItems.Count);
            foreach (ListViewItem anItem in selectedItems)
            {
                moveItems.Add(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                extractionRuleListView.Items.Remove(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                extractionRuleListView.Items.Insert(insertIndex, anItem);
                insertIndex++;
            }
        }

        private void extractionRuleListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                ExtractionRuleListViewItem item = (ExtractionRuleListViewItem)e.Item;
                _lastSelectedItem = item;

                fileNamePatternTextBox.Text = item.Pattern;
                if (DynamicColumnExtractionRule.SpliterExtractorName.Equals(item.ExtractorName))
                {
                    splitRadioButton.Checked = true;
                }
                else
                {
                    regexRadioButton.Checked = true;
                }
                
                LoadColumnDefinitions(item);
            }
            else
            {
                ExtractionRuleListViewItem item = (ExtractionRuleListViewItem)e.Item;

                SaveColumnDefinitions(item);
            }
        }

        private void extractionRuleListView_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked)
            {
                foreach (ListViewItem aItem in extractionRuleListView.CheckedItems)
                {
                    if (aItem != e.Item)
                        aItem.Checked = false;
                }
            }
        }

        private void extractionRuleListView_SizeChanged(object sender, EventArgs e)
        {
            ListView self = sender as ListView;
            if (self.Width > 5)
            {
                patternColumnHeader.Width = self.Width - 65;
            }
        }

        private void columnDefinitionAddButton_Click(object sender, EventArgs e)
        {
            string columnName = columnNameTextBox.Text;
            string columnHeader = columnHeaderTextBox.Text;
            int columnWidth = (int)columnWidthNumericUpDown.Value;
            bool columnEnabled = columnDefinitionEnabledCheckBox.Checked;
            if (String.IsNullOrEmpty(columnName) || String.IsNullOrEmpty(columnHeader))
            {
                MessageBox.Show("请输入有效的内容", "添加失败");
                return;
            }

            ColumnDefinitionListViewItem item = new ColumnDefinitionListViewItem(
                columnName, columnHeader, columnWidth, columnEnabled);
            columnDefinitionListView.Items.Add(item);
        }

        private void columnDefinitionRemoveButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = columnDefinitionListView.SelectedItems;
            if (items.Count != 0)
            {
                foreach (ListViewItem aItem in items)
                {
                    columnDefinitionListView.Items.Remove(aItem);
                }
            }
            else
            {
                MessageBox.Show("请选择需要删除的项目", "删除失败");
            }
        }

        private void columnDefinitionModifyButton_Click(object sender, EventArgs e)
        {
            string columnName = columnNameTextBox.Text;
            string columnHeader = columnHeaderTextBox.Text;
            int columnWidth = (int)columnWidthNumericUpDown.Value;
            bool columnEnabled = columnDefinitionEnabledCheckBox.Checked;
            if (String.IsNullOrEmpty(columnName) || String.IsNullOrEmpty(columnHeader))
            {
                MessageBox.Show("请输入有效的内容", "修改失败");
                return;
            }

            ListView.SelectedListViewItemCollection items = columnDefinitionListView.SelectedItems;
            if (items.Count == 1)
            {
                ColumnDefinitionListViewItem item = (ColumnDefinitionListViewItem)items[0];
                item.ColumnName = columnName;
                item.ColumnHeader = columnHeader;
                item.ColumnWidth = columnWidth;
                item.ColumnEnabled = columnEnabled;
            }
            else
            {
                MessageBox.Show("请选择一项修改", "修改失败");
                return;
            }
        }

        private void columnDefinitionUpButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedItems = columnDefinitionListView.SelectedItems;
            if (selectedItems.Count == 0)
            {
                return;
            }

            int insertIndex = selectedItems[0].Index;
            if (insertIndex > 0)
            {
                insertIndex--;
            }

            List<ListViewItem> moveItems = new List<ListViewItem>(selectedItems.Count);
            foreach (ListViewItem anItem in selectedItems)
            {
                moveItems.Add(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                columnDefinitionListView.Items.Remove(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                columnDefinitionListView.Items.Insert(insertIndex, anItem);
                insertIndex++;
            }
        }

        private void columnDefinitionDownButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedItems = columnDefinitionListView.SelectedItems;
            if (selectedItems.Count == 0)
            {
                return;
            }

            int insertIndex = selectedItems[selectedItems.Count-1].Index;
            if (insertIndex < columnDefinitionListView.Items.Count - 1)
            {
                insertIndex++;
            }
            insertIndex++;
            insertIndex = insertIndex - selectedItems.Count;

            List<ListViewItem> moveItems = new List<ListViewItem>(selectedItems.Count);
            foreach (ListViewItem anItem in selectedItems)
            {
                moveItems.Add(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                columnDefinitionListView.Items.Remove(anItem);
            }

            foreach (ListViewItem anItem in moveItems)
            {
                columnDefinitionListView.Items.Insert(insertIndex, anItem);
                insertIndex++;
            }
        }

        private void columnDefinitionListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                ColumnDefinitionListViewItem item = (ColumnDefinitionListViewItem)e.Item;
                columnNameTextBox.Text = item.ColumnName;
                columnHeaderTextBox.Text = item.ColumnHeader;
                columnWidthNumericUpDown.Value = item.ColumnWidth;
                columnDefinitionEnabledCheckBox.Checked = item.ColumnEnabled;
            }
        }
    }
}
